import actionsType from './actionsType'
const initState = {
  tabList: [],
  // 16.实现历史搜索数据的持久化刷新页面不丢失数据
  searchHistory: JSON.parse(localStorage.getItem('hist')) || []
}
// 7.使用状态管理容器redux，异步请求接口数据并实现列表数据的持久化
const reducer = (state = initState, action) => {
  const { type, payload } = action
  const {searchHistory} = state
  switch(type){
    case actionsType.SET_TAB_LIST:  
      return {
        ...state,
        tabList: payload
      };
    case actionsType.ADD_SEARCH_HISTORY: 
     // 16.实现历史搜索数据的持久化刷新页面不丢失数据
      localStorage.setItem('hist', JSON.stringify([...searchHistory, payload]))
      return {
        ...state,
        searchHistory: [...searchHistory, payload]
      }
    default: 
      return { ...state }
  }
}


export default reducer